# _*_ coding: utf-8 _*_

from selenium import webdriver
from os import getcwd
import time
import datetime
import json
import random
import configparser
from urllib.request import Request, urlopen
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options


# 全局url
LOGIN_PAGE = 'http://office.teacher.com.cn/views/loginAndRegister/login/login.html'   
module_n=0
study_text=[]
study_url = []
start_num=0


print("开始执行你的测试用例！")
 
 
#第二个输入这个：隐藏式启动谷歌浏览器执行UI测试用例
chrome_options = Options()
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless')
chrome_options.add_argument("--mute-audio")#静音
chrome_options.add_argument('log-level=3')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_webdriver_location=getcwd()+"/Chrome/Application/chromedriver.exe"
#　输入chrome路径
browser = webdriver.Chrome(executable_path=chrome_webdriver_location,options=chrome_options)

browser.implicitly_wait(20)


#　使用远程培训的用户名与密码登录    
def login(name,password):
    browser.get(LOGIN_PAGE)
    time.sleep(3)
    name_input = browser.find_element(By.XPATH,"//input[@id='mobile']")
    password_input = browser.find_element(By.XPATH,"//input[@id='password']")
    login_btn = browser.find_element(By.XPATH,"//span[@id='login']")
    name_input.send_keys(name)
    password_input.send_keys(password)
    browser.execute_script("arguments[0].click();",login_btn)
    enter_study = browser.find_elements(By.XPATH,"//div[@class='button-item button-item-hover']")
    if len(enter_study)==0:
        print ("登录失败")
        browser.quit()
    else:
        print("模拟登录完毕\n")
    
def require_list():
    #module_arr = browser.find_elements(By.XPATH,"//a[@class='list-title' and target='view_window']")
    module_arr = browser.find_elements(By.XPATH,"//div[@class='con']//a[@class='list-title']")
    module_n=len(module_arr)
    #print(module_n)
    for module_ele in module_arr:
        study_text.append(module_ele.get_attribute('innerText'))
        study_url.append(module_ele.get_attribute('href'))
    for study_t in study_text:
        print (study_t)
    print("\n")
    
    
if __name__ == '__main__':#此处填写自己的用户名与密码
    name = input("输入登录的用户名:")
    print("你的用户名为:"+name)
    passwords=input("输入密码:")
    login(name,passwords)
    enter_study = browser.find_element(By.XPATH,"//div[@class='button-item button-item-hover']")
    browser.execute_script("arguments[0].click();",enter_study)
    time.sleep(20)
    require_list()
    for index in range(start_num,len(study_url)):
        browser.get(study_url[index])
        time.sleep(10)
        required_min_str = browser.find_element(By.XPATH,"//span[@id='courseStudyBestMinutesNumber']").get_attribute('innerText')
        got_min_str = browser.find_element(By.XPATH,"//span[@id='courseStudyMinutesNumber']").get_attribute('innerText')
        required_minute = int(required_min_str)
        got_minute = int(got_min_str)
        flag = 0
        print("当前模块的名称:"+study_text[index])
        print("需要的分数:"+str(required_minute))
        print("已学完的分数"+str(got_minute)+'\n')
        while got_minute<required_minute:
            refresh_btn_arr = browser.find_elements(By.XPATH,"//button[@class='btn studyCourseTimeRefresh']")
            refresh_p_arr = browser.find_elements(By.XPATH,"//p[@class='studyCourseTimeRefresh']")
            replay_btn_arr = browser.find_elements(By.XPATH,"//div[@id='replaybtn']")
            study_tip_arr = browser.find_elements(By.XPATH,"//a[@class='layui-layer-btn0']")
            flag=flag + 1
            got_min_str = browser.find_element(By.XPATH,"//span[@id='courseStudyMinutesNumber']").get_attribute('innerText')
            required_minute = int(required_min_str)
            got_minute = int(got_min_str)
            if len(refresh_btn_arr) !=0:
                browser.execute_script("arguments[0].click();",refresh_btn_arr[0])
            if len(refresh_p_arr) !=0:
                browser.execute_script("arguments[0].click();",refresh_p_arr[0])
            if len(replay_btn_arr) !=0:
                browser.execute_script("arguments[0].click();",replay_btn_arr[0])
            if len(study_tip_arr) != 0:
                browser.execute_script("arguments[0].click();",study_tip_arr[0])
            time.sleep(30)
            
            if flag>8:
                flag = 0
                print("当前模块的名称:"+study_text[index])
                print("需要的分数:"+str(required_minute))
                print("已学完的分数"+str(got_minute)+'\n')
        print("已学完了模块:"+study_text[index]+got_min_str+"分钟\n")
    browser.quit()
    
            
